package ShareYourWay.rest;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import ShareYourWay.Mapped.MappedMeetUp;
import ShareYourWay.Mapped.MappedUser;
import ShareYourWay.functions.App;
import ShareYourWay.project.Meetup;
import ShareYourWay.project.University;
import ShareYourWay.project.User;
import ShareYourWay.project.Usermeetup;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;


@Path("/api")
public class Api_rest implements Serializable {
	
	Pruebas_Meet lista = new Pruebas_Meet();
	User user = new User ();
	User u = new User();
	App app =  new App ();
	
	
	@Path("/verifyLogin")
	@GET
	//@Consumes("application/json")
	@Produces(MediaType.APPLICATION_JSON)
	public String login (@QueryParam("mail") String mail, @QueryParam("password") String password) {
		
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@ - REST mail:"+mail+" pass: "+password);
	
		user.setMail(mail);
		user.setPassword(password);	

		Gson  gson = new Gson();
		
		u=app.Login(user);
		
		User u1 = new User();
		u1.setCar(u.isCar());
		u1.setCity(u.getCity());
		u1.setId(u.getId());
		u1.setMail(u.getMail());
		u1.setName(u.getName());
		u1.setNick(u.getNick());
		u1.setPassword(u.getPassword());
		u1.setPhone(u.getPhone());
		u1.setScore(u.getScore());
		u1.setSex(u.getSex());
		u1.setSurname(u.getSurname());
		System.out.println("@@@@@@@@@@@@@@@@@ user"+u.getName());
		
		String user = gson.toJson(u1);
		
		return user;		
			
	}	
		
	@Path("/register")
	@POST
	@Consumes("application/json")	
	public void register(String data) {		
		
		MappedUser mp = new Gson().fromJson(data, MappedUser.class);
		
		System.out.println("Nombre"+mp.getName());
	
		
		User u = new User(mp.getName(), mp.getSurname(), mp.getPassword(), mp.getNick(), mp.isCar(), mp.getPhone(), mp.getMail(), mp.getCity(),mp.getSex());
		
		app.addUser(u);		  
	
 }
	
	@Path("/listmeetup")
	@GET	
	@Produces(MediaType.APPLICATION_JSON)	
	public String listmeets (@QueryParam("iduser") String iduser) {
		
		Gson  gson = new Gson ();
		int id = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id);
		
		ArrayList<Meetup>listameet=(ArrayList<Meetup>) app.listMeet(u);	
	
				
		List<MappedMeetUp> list = new ArrayList<MappedMeetUp>();
		
	
		
		for(int i=0;i<listameet.size();i++){
			
			
			MappedMeetUp met = new MappedMeetUp();		

			met.setIdMeetup(listameet.get(i).getIdMeetup());
			met.setDestination(listameet.get(i).getDestination());	
			met.setOrigin(listameet.get(i).getOrigin());
			met.setSlots(listameet.get(i).getSlots());
			met.setTimeMeet(listameet.get(i).getTimeMeet());			
			met.setVehicle(listameet.get(i).getVehicle());
			
			System.out.println("-----------------"+met.getDestination());
			
			list.add(met);
			
		}
		
		System.out.println("    tlist"+list.size());
		
		System.out.println("    tlist"+list.size());
		String a = gson.toJson(list);		

		
		System.out.println("--------Lista: "+a);		

		
		return a;		
			
	}	
	
	
	@Path("/joinMeet")
	@POST
	@Consumes("application/json")	
	public void joinMeet(@QueryParam("idmeet") String idmeet,@QueryParam("iduser") String iduser) {		
		
		int id = Integer.parseInt(idmeet);
		
		int id2 = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id2);
		app.insertusermeetup(id,u);
 
	
 }
	@Path("/hideMeet")
	@POST
	@Consumes("application/json")	
	public void hideMeet(@QueryParam("idmeet") String idmeet,@QueryParam("iduser") String iduser) {		
		
		int id = Integer.parseInt(idmeet);
		
		int id2 = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id2);
		app.hideUserMeetup(id,u);
 
	
 }
	
	
	@Path("/listMeetbyOD")
	@GET	
	@Produces(MediaType.APPLICATION_JSON)	
	public String listMeetbyOD (@QueryParam("origin") String origin,@QueryParam("destination") String destination,@QueryParam("iduser") String iduser) {
		
		Gson  gson = new Gson ();
		int id = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id);
		
		ArrayList<Meetup>listameet=(ArrayList<Meetup>) app.listMeetbyOD(origin, destination, u);	
	
				
		List<MappedMeetUp> list = new ArrayList<MappedMeetUp>();
		
	
		
		for(int i=0;i<listameet.size();i++){
			
			
			MappedMeetUp met = new MappedMeetUp();		

			met.setIdMeetup(listameet.get(i).getIdMeetup());
			met.setDestination(listameet.get(i).getDestination());	
			met.setOrigin(listameet.get(i).getOrigin());
			met.setSlots(listameet.get(i).getSlots());
			met.setTimeMeet(listameet.get(i).getTimeMeet());			
			met.setVehicle(listameet.get(i).getVehicle());
			
			System.out.println("-----------------"+met.getDestination());
			
			list.add(met);
			
		}
		
		System.out.println("    tlist"+list.size());
		
		System.out.println("    tlist"+list.size());
		String a = gson.toJson(list);		

		
		System.out.println("--------Lista: "+a);		

		
		return a;		
			
	}	
	
	
	
	@Path("/listMeetbyDestination")
	@GET	
	@Produces(MediaType.APPLICATION_JSON)	
	public String listMeetbyDestination (@QueryParam("destination") String destination,@QueryParam("iduser") String iduser) {
		
		Gson  gson = new Gson ();
		int id = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id);
		
		ArrayList<Meetup>listameet=(ArrayList<Meetup>) app.listMeetbyDestination(destination, u);
	
				
		List<MappedMeetUp> list = new ArrayList<MappedMeetUp>();
		
	
		
		for(int i=0;i<listameet.size();i++){
			
			
			MappedMeetUp met = new MappedMeetUp();		

			met.setIdMeetup(listameet.get(i).getIdMeetup());
			met.setDestination(listameet.get(i).getDestination());	
			met.setOrigin(listameet.get(i).getOrigin());
			met.setSlots(listameet.get(i).getSlots());
			met.setTimeMeet(listameet.get(i).getTimeMeet());			
			met.setVehicle(listameet.get(i).getVehicle());
			
			System.out.println("-----------------"+met.getDestination());
			
			list.add(met);
			
		}
		
		System.out.println("    tlist"+list.size());
		
		System.out.println("    tlist"+list.size());
		String a = gson.toJson(list);		

		
		System.out.println("--------Lista: "+a);		

		
		return a;		
			
	}	
	
	
	@Path("/listMeetbyVehicle")
	@GET	
	@Produces(MediaType.APPLICATION_JSON)	
	public String listMeetbyVehicle (@QueryParam("vehicle") String vehicle,@QueryParam("iduser") String iduser) {
		
		Gson  gson = new Gson ();
		int id = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id);
		
		ArrayList<Meetup>listameet=(ArrayList<Meetup>) app.listMeetbyVehicle(vehicle, u);
	
				
		List<MappedMeetUp> list = new ArrayList<MappedMeetUp>();
		
	
		
		for(int i=0;i<listameet.size();i++){
			
			
			MappedMeetUp met = new MappedMeetUp();		

			met.setIdMeetup(listameet.get(i).getIdMeetup());
			met.setDestination(listameet.get(i).getDestination());	
			met.setOrigin(listameet.get(i).getOrigin());
			met.setSlots(listameet.get(i).getSlots());
			met.setTimeMeet(listameet.get(i).getTimeMeet());			
			met.setVehicle(listameet.get(i).getVehicle());
			
			System.out.println("-----------------"+met.getDestination());
			
			list.add(met);
			
		}
		
		System.out.println("    tlist"+list.size());
		
		System.out.println("    tlist"+list.size());
		String a = gson.toJson(list);		

		
		System.out.println("--------Lista: "+a);		

		
		return a;		
			
	}	
	
	
	@Path("/listMyMeetups")
	@GET	
	@Produces(MediaType.APPLICATION_JSON)	
	public String listMyMeetups (@QueryParam("iduser") String iduser) {
		
		Gson  gson = new Gson ();
		int id = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id);
		
		ArrayList<Meetup>listameet=(ArrayList<Meetup>) app.listMyMeet(u);	
	
				
		List<MappedMeetUp> list = new ArrayList<MappedMeetUp>();
		
	
		
		for(int i=0;i<listameet.size();i++){
			
			
			MappedMeetUp met = new MappedMeetUp();		

			met.setIdMeetup(listameet.get(i).getIdMeetup());
			met.setDestination(listameet.get(i).getDestination());	
			met.setOrigin(listameet.get(i).getOrigin());
			met.setSlots(listameet.get(i).getSlots());
			met.setTimeMeet(listameet.get(i).getTimeMeet());			
			met.setVehicle(listameet.get(i).getVehicle());
			
			System.out.println("-----------------"+met.getDestination());
			
			list.add(met);
			
		}
		
		System.out.println("    tlist"+list.size());
		
		System.out.println("    tlist"+list.size());
		String a = gson.toJson(list);		

		
		System.out.println("--------Lista: "+a);		

		
		return a;		
			
	}	
	
	
	@Path("/listMeetupsOn")
	@GET	
	@Produces(MediaType.APPLICATION_JSON)	
	public String listMeetupsOn (@QueryParam("iduser") String iduser) {
		
		Gson  gson = new Gson ();
		int id = Integer.parseInt(iduser);
		
		User u = new User();
		
		u.setId(id);
		
		ArrayList<Meetup>listameet=(ArrayList<Meetup>) app.ListMeetupOn(u);	
	
				
		List<MappedMeetUp> list = new ArrayList<MappedMeetUp>();
		
	
		
		for(int i=0;i<listameet.size();i++){
			
			
			MappedMeetUp met = new MappedMeetUp();		

			met.setIdMeetup(listameet.get(i).getIdMeetup());
			met.setDestination(listameet.get(i).getDestination());	
			met.setOrigin(listameet.get(i).getOrigin());
			met.setSlots(listameet.get(i).getSlots());
			met.setTimeMeet(listameet.get(i).getTimeMeet());			
			met.setVehicle(listameet.get(i).getVehicle());
			
			System.out.println("-----------------"+met.getDestination());
			
			list.add(met);
			
		}
		
		System.out.println("    tlist"+list.size());
		
		System.out.println("    tlist"+list.size());
		String a = gson.toJson(list);		

		
		System.out.println("--------Lista: "+a);		

		
		return a;		
			
	}	
	/*
	
	@Path ("/filtermeet")
	@POST
	//@Produces("application/json")

	public String buscar(@QueryParam("vehicle") String vehicle, @QueryParam("origin") String origin, @QueryParam("destination") String destination){
		System.out.println("1");
		System.out.println(vehicle+"  ,"+origin+" , "+destination);
		
		Meetup m = new Meetup();
		m.setDestination(destination);
		m.setOrigin(origin);
		m.setVehicle(vehicle);
		
		m.setTimeMeet("ferfw");
		m.setSlots(1);
		
		System.out.println(m.getDestination());
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		
		session.save(m);
		
		session.getTransaction().commit();
		System.out.println("OK -- Clientes y productos cargados correctamente!");
		session.close();
		return "ok";}
	
   @Path("/listar")
   @GET	
	public String listar() {
		
		lista.Listar();		
		return "Se ha listado en consola!";
	
		  
	
	}
	
	*/

}
